#include <QCoreApplication>

extern "C" {
#include "sdl2/SDL.h"
}

//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

//Starts up SDL and creates window 初始化SDL并创建窗口
bool init();

//Loads media  加载多媒体文件
bool loadMedia();

//Frees media and shuts down SDL  释放多媒体文件并关闭SDL
void close();

//The window we'll be rendering to  初始化我们将要渲染的窗口
SDL_Window* gWindow = NULL;

//The surface contained by the window  窗口包含的surface
SDL_Surface* gScreenSurface = NULL;

//The image we will load and show on the screen  我们将会载入并在屏幕上展示的图片
SDL_Surface* gHelloWorld = NULL;

bool init()
{
    //Initialization flag 初始化标记变量
    bool success = true;

    //Initialize SDL
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
        success = false;
    }
    else
    {
        //Create window
        gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
        if( gWindow == NULL )
        {
            printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
            success = false;
        }
        else
        {
            //Get window surface
            gScreenSurface = SDL_GetWindowSurface( gWindow );
        }
    }

    return success;
}

bool loadMedia()
{
    //Loading success flag
    bool success = true;

    //Load splash image
    gHelloWorld = SDL_LoadBMP( "D:/code/SDL/02display_Image/hello_world.bmp" );
    if( gHelloWorld == NULL )
    {
        printf( "Unable to load image %s! SDL Error: %s\n", "02_getting_an_image_on_the_screen/hello_world.bmp", SDL_GetError() );
        success = false;
    }

    return success;
}

void close()
{
    //Deallocate surface
    SDL_FreeSurface( gHelloWorld );
    gHelloWorld = NULL;

    //Destroy window
    SDL_DestroyWindow( gWindow );
    gWindow = NULL;

    //Quit SDL subsystems
    SDL_Quit();
}

int main( int argc, char* args[] )
{
    //Start up SDL and create window
    if( !init() )
    {
        printf( "Failed to initialize!\n" );
    }
    else
    {
        //Load media
        if( !loadMedia() )
        {
            printf( "Failed to load media!\n" );
        }
        else
        {
            //Apply the image
            /*!
                @fn     : SDL_BlitSurface 将一个表面（Surface）上的像素数据复制到另一个表面上
                @param  : src：源表面，包含要复制的像素数据。
                @param  : srcrect：源表面上的矩形区域的位置和大小，如果为nullptr则表示整个表面。
                @param  : dst：目标表面，将像素数据复制到这个表面上。
                @param  : dstrect：目标表面上的矩形区域的位置和大小，如果为nullptr则表示整个表面。
            */
            SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );

            //Update the surface
            SDL_UpdateWindowSurface( gWindow );

            SDL_Delay( 2000 );
            //Hack to get window to stay up
//            SDL_Event e; bool quit = false; while( quit == false ){ while( SDL_PollEvent( &e ) ){ if( e.type == SDL_QUIT ) quit = true; } }
        }
    }

    //Free resources and close SDL
    close();

    return 0;
}
